package Bluetooth.find;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.UUID;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothServerSocket;
import android.bluetooth.BluetoothSocket;
import android.os.Handler;
import android.widget.TextView;

public class BTServerThread extends Thread implements Runnable{
    public TextView tv1;
    public BluetoothAdapter adap;
    private InputStream inputStream=null;
    private OutputStream outputStream=null;
    static UUID UUID_RFCOMM_GENERIC = new UUID(0x0000110100001000L,0x800000805F9B34FBL);
    private final BluetoothServerSocket btServerSocket;
    private Handler handler= new Handler();
	BTServerThread(TextView tv1,BluetoothAdapter adap)
	{
		this.tv1=tv1;
		this.adap=adap;
		BluetoothServerSocket tmp = null;
        try {
            // MY_UUID is the app's UUID string, also used by the client code
           tmp = adap.listenUsingRfcommWithServiceRecord("Bluetooth", UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
        	//tmp = adap.listenUsingRfcommWithServiceRecord("Bluetooth", UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
        } catch (IOException e) { }
        btServerSocket = tmp;
	}
	public void post(final String s)
    {
            handler.post(new Runnable() {
        public void run() {
            tv1.setText("\n"+s);
        }
    });
    }
	@Override
	public void run() {
		//tv1.setText("in server");
		BluetoothSocket socket = null;
        // Keep listening until exception occurs or a socket is returned
        
            try {
                socket = btServerSocket.accept();
             //  tv1.setText("connection established");
                //BluetoothActivity.printing("connection established");
                post("connection established");
            } catch (IOException e) {
               
            }
            if(socket!=null)
            {
	            InputStream tmpIn = null;
	            OutputStream tmpOut = null;
	     
	            // Get the input and output streams, using temp objects because
	            // member streams are final
	            try {
	                tmpIn = socket.getInputStream();
	                tmpOut = socket.getOutputStream();
	            } catch (IOException e) { }
	            try{
	            inputStream = tmpIn;
	            outputStream = tmpOut;
	            }
	            catch(Exception e)
	            {
	            	e.printStackTrace();
	            }
	        	byte[] buffer = new byte[50];  // buffer store for the stream
	            int bytes; // bytes returned from read()
	        	
	                try {
	                    // Read from the InputStream
	                    bytes = inputStream.read(buffer);
	                    // Send the obtained bytes to the UI activity
	              //   tv1.setText(new String(buffer));
	                //    BluetoothActivity.printing(new String(buffer));
	                   
	                    String st= new String(buffer);
	                    String str1= st.substring(0, buffer.length);
	                    post(str1);																																												
	                 
	                  
	                } catch (IOException e) {
	                    
	                }
	                try {
	                	 String msg1=" Hi i recieved ur msg";
	                     byte[] writebytes = msg1.getBytes();
	                    outputStream.write(writebytes);
	                }
	                catch ( Exception e)
	                {
	                
	                }
            }
                
                
            
         //  if (socket != null) {
                // Do work to manage the connection (in a separate thread)
               
                try {
					btServerSocket.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
              
          //  }
        }
		
		
	
	
}
